package com.lzy.controller;

import com.lzy.common.R;
import com.lzy.constant.UserConstant;
import com.lzy.security.LoginUser;
import com.lzy.service.ISysLoginService;
import com.lzy.vo.UserLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author lyw
 * @Date 2023/2/1-上午10:04
 */
@Api(tags = "测试模块")
@RestController
public class SysLoginController {

  @Autowired
  private ISysLoginService sysLoginService;

//  @Log(title = "登陆用户", businessType = BusinessType.LOGIN, isSaveRequestParams = true)
  @PostMapping("/login")
  @ApiOperation("用户登陆")
  public R login(@RequestBody UserLoginVo userLoginVo) throws IOException {

    String token = sysLoginService.login(userLoginVo);
    LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return R.ok().put("token", token).put("user", loginUser);
  }

  @PostMapping(UserConstant.REFRESH_URI)
  @ApiOperation("刷新token")
  public R refresh() throws IOException {
    String token = sysLoginService.refreshToken();
    return R.ok().put("token", token);
  }

  @PostMapping("getInfo")
  @ApiOperation("用户信息")
  public R getUserInfo() {
    return R.ok();
  }
}
